<template>
	<view class="container">
		<view class="publish-card">
			<view class="card-header">
				<text class="card-title">发布心愿</text>
			</view>
			
			<view class="form-area">
				<view class="text-area">
					<textarea 
						class="wish-input" 
						v-model="wishContent" 
						placeholder="写下你的心愿..." 
						maxlength="200"
						:auto-height="true"
					></textarea>
					<view class="word-count">
						<text>{{wishContent.length}}/200</text>
					</view>
				</view>
				
				<view class="option-card">
					<view class="option">
						<text class="option-label">是否公开</text>
						<switch 
							:checked="isPublic" 
							@change="handleSwitchChange" 
							color="#6159e6"
						/>
					</view>
					<view class="tip">
						<text>公开的心愿将显示在心愿广场，所有人可见</text>
					</view>
				</view>
			</view>
		</view>
		
		<view class="btn-card">
			<button 
				class="publish-btn" 
				:disabled="!wishContent.trim()" 
				@click="handlePublish"
			>发布心愿</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wishContent: '',
				isPublic: true
			}
		},
		onLoad() {
			// 检查登录状态
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return;
			}
		},
		onShow() {
			// 检查登录状态
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return;
			}
		},
		methods: {
			handleSwitchChange(e) {
				this.isPublic = e.detail.value;
			},
			
			handlePublish() {
				if (!this.wishContent.trim()) {
					uni.showToast({
						title: '请输入心愿内容',
						icon: 'none'
					});
					return;
				}
				
				// 显示加载中
				uni.showLoading({
					title: '发布中...'
				});
				
				// 调用API发布心愿
				this.$api.createWish({
					content: this.wishContent,
					is_public: this.isPublic
				}).then(res => {
					uni.hideLoading();
					
					// 发布成功
					uni.showToast({
						title: '发布成功',
						icon: 'success'
					});
					
					// 跳转到首页
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 1500);
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: '发布失败，请重试',
						icon: 'none'
					});
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f5f7fa;
		padding: 30rpx;
		box-sizing: border-box;
	}
	
	.publish-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		overflow: hidden;
		margin-bottom: 30rpx;
		flex: 1;
	}
	
	.card-header {
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f2f5;
	}
	
	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}
	
	.form-area {
		padding: 30rpx;
	}
	
	.text-area {
		background-color: #f9fafc;
		border-radius: 16rpx;
		padding: 20rpx;
		position: relative;
		margin-bottom: 40rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.wish-input {
		width: 100%;
		min-height: 300rpx;
		font-size: 32rpx;
		color: #333;
		line-height: 1.5;
		background-color: transparent;
	}
	
	.word-count {
		text-align: right;
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}
	
	.option-card {
		background-color: #f9fafc;
		border-radius: 16rpx;
		padding: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.option {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10rpx;
	}
	
	.option-label {
		font-size: 32rpx;
		color: #333;
	}
	
	.tip {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}
	
	.btn-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}
	
	.publish-btn {
		width: 100%;
		height: 90rpx;
		background: linear-gradient(135deg, #3b73d9, #6159e6);
		color: #fff;
		border-radius: 45rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 8rpx 20rpx rgba(97, 89, 230, 0.25);
		border: none;
		transition: all 0.3s ease;
	}
	
	.publish-btn:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 10rpx rgba(97, 89, 230, 0.2);
	}
	
	.publish-btn[disabled] {
		background: linear-gradient(135deg, #cccccc, #aaaaaa);
		box-shadow: none;
	}
</style> 